<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Html_Filter_Abstract **/
require_once 'Zead/Html/Filter/Abstract.php';

/**
 * Abstract class Zead_Html_Filter_Code
 *
 * @category   Zead
 * @package    Zead_Html
 * @subpackage Zead_Html_Filter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
abstract class Zead_Html_Filter_Code_Abstract extends Zead_Html_Filter_Abstract 
{
	/**
	 * Valor com o nome da linguagem a ser mapeado
	 * @var string
	 */
	protected $language = null;
	
	/**
	 * Aplica o filtro da linguagem. Metodo global para todas linguagens
	 * Necessita apenas que o atributo language esteja setado e a o metodo
	 * parse implementado.
	 * @return 
	 */
	public function filter() {
		if (null === $this->language) return;
		
		// Listando as tags PRE com a classe code
		$this->html->select('pre.code')
				->select(array($this,'select'))
					->each(array($this,'parse'));
	}

	/**
	 * Iterator para retornar o elemento somente se ele for uma tag pre
	 * com a classe da linguagem de programacao escolhida
	 * @param Zead_Html_Element
	 * @return bool
	 */
	public function select($element) {
		return $element->hasClassName($this->language);
	}

	/** 
	 * Faz o parser no código que consta no elemento e atualiza
	 * com seus pedaços de códigos coloridos.
	 * @param Zead_Html_Element $element
	 * @return void
	 *
	abstract public function parse($element);

	/**
	 * Atualizando o elemento com o código separado em pedações de array
	 * com seu respectivo nome tipo de e seu codigo
	 *
	 * @param Zead_Html_Element $element
	 * @param array $code
	 * @return void
	 */
	protected function update($element, $code) {
		$ol = $this->html->createElement('ol');
		
		// Append styles
		foreach ($code as $line) {
			$li = $ol->appendChild($this->html->createElement('li'));
			foreach ($line as $piece) {
				$span = $li->appendChild($this->html->createElement('span'));
				$span->addClassName($piece['type']);
				$span->nodeValue = $piece['value'];
			}
		}
		
		// Append to element
		$element->parentNode->replaceChild($ol,$element);
		$ol->setAttribute('class', 'code '. $this->language);
	}
}
